package com.heriparid.fragmentinjection.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ViewGroup;
import butterknife.Views;

import com.heriparid.fragmentinjection.BaseFragmentActivity;
import com.heriparid.fragmentinjection.R;
import com.heriparid.fragmentinjection.ui.fragments.ItemListFragment;

public class MainActivity extends BaseFragmentActivity {
	
	private static final String TAG = MainActivity.class.getSimpleName();

	ViewGroup mItemListLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		Log.v(TAG, "onCreate: savedInstanceState is null: " + String.valueOf(savedInstanceState == null));
		
		setContentView(R.layout.main_layout);
		super.onCreate(savedInstanceState);
		
		// Restore state
		if(savedInstanceState != null){
			// The fragment manager will handle restoring them if we are being
			// restored from a saved state
		}
		// If this is the first creation of the activity, add fragments to it
		else{
			// if layout has a container for the item list fragment, add it.
			mItemListLayout = Views.findById(this, R.id.activity_main_item_list);
			if(mItemListLayout != null){
				Log.i(TAG, "onCreate: adding item list fragment to MainActivity");
				
				// Add item list fragment to the activity's container layout
				
				ItemListFragment fragment = ItemListFragment.newInstance();
				injectFragment(fragment);
				
				FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
				fragmentTransaction.replace(mItemListLayout.getId(), fragment);
				
				fragmentTransaction.commit();
			}
		}
	}
}
